// alert(window.MFC)

// let hasI18nInit=(false)
export const i18nMixin =function(p){
    const {reactive,ref,computed,onBeforeUnmount}=window.MFC;
    let tt=reactive({})
    window.$v3.doNothing('p---:',p)
    let localeMap=null;
    p.then((res)=>{
    // p.__PROM.then((res)=>{
        localeMap=res;
        for(let i in res){
            tt[i]=res[i]
        }
        window.$v3.doNothing('lang res---:',res)
    })

    
    
    let lang=ref(window.$v3.appLang)
    let t=computed(()=>tt[lang.value]||{})
    let langSwitchFn=(val)=>{
        lang.value=val;
        window.$v3.doNothing('lang val---:',val)
    }

    // onBeforeUnmount
    // if(!hasI18nInit){
        window.$v3.$bus.$on('B_APP_SET_LANG',langSwitchFn)
        onBeforeUnmount(()=>{
            window.$v3.doNothing('will onBeforeUnmount');
            window.$v3.$bus.$off('B_APP_SET_LANG',langSwitchFn)
        })
        // hasI18nInit=true
    // }

    // function ttt(key){
    //     try{
    //         return t[key]
    //     }catch(e){
    //         return ''
    //     }
       
    // }
    // a.aa=33333333;
    return {
        lang:lang,
        t:t,
        // ttt,
        // tt
        // a:a
    }
    // let lang=ref(window.$v3.appLang)
}

export const storeMixin=function(obj){
    const {watch}=window.MFC;
    const {reactive}=window.MFC;

    let store$=reactive({
        ...window.$v3.$store,
        ...obj
    })



    for(let key in obj){
        window.$v3.$store[key]=obj[key]
        window.MFC.onUnmounted(() => {
            // window.$v3.doNothing('clear store key '+key+' after unmount');
            delete store$[key];
            delete window.$v3.$store[key]  
        })
    }

    


    // for(let i in obj){
    //     window.$v3.$store[i]=obj[i]
    // }

    // watch(store,function(newVal){
    //     for(let i in obj){
    //         window.$v3.$store[i]=newVal[i]
    //     }
    //     window.$v3.doNothing('store in watch :',arguments)
    // })


    const updateStore$=(key,val)=>{

       

        setTimeout(()=>{
            // window.$v3.doNothing('set '+ key+' for store')
            window.$v3.$store[key]=store$[key]=val
            // window.$v3.$store[i]=store[key]=val
        })

        
        
    }

    return {
        store$,
        updateStore$
    }

}

// export const loggerMixin=function(){
//     return {
//         log:window.window.$v3.doNothing,
//         win:window,
//         alert:window.alert,
//     }
// }

// export const optionsApiDebugMixin=function(){
//     return {
//         mounted(){
            
//         }
//     }
// }

export const moduleMixin=function(config,cb){
    const {ref,reactive}=window.MFC;
let map={}
for(let i in config){
    window.$v3.doNothing('config i----:',config[i])
    map[i]=reactive({})
    config[i].then((res)=>{
    // config[i].__PROM.then((res)=>{
        window.$v3.doNothing('ressssssssssss-----------:',res)
        if(typeof(res)=='object'){
            for(let j in res){
                map[i][j]=res[j]
                // map[i].value[j]=res[j]
            }
        }else{
            map[i].exports=res
            // map[i].value.exports=res
        }
        return res;
        
    })
    window.$v3.doNothing('config[i]--:',config[i])
}

let cbRes=cb(map)
window.$v3.doNothing('cb res---:',cbRes)
if(!Array.isArray(cbRes) && cbRes!==null && typeof(cbRes)=='object'){
    for(let k in cbRes){
        map[k]=cbRes[k]
    }
}

return map

}


// export const printMixin=function(obj,logInDevMode=true){

//     if(location.hostname=='127.0.0.1' && logInDevMode){
//         console['log']('obj in print mixin---:',obj)

        
//     }
    

//     return obj;

// }

export const windowResizeMixin=function(obj,fn){
    obj=obj||{value:null}
    
    let resizeFn=function(e){
        obj.value=fn(e);
    }
    resizeFn();
    window.addEventListener('resize',resizeFn);
    window.MFC.onBeforeUnmount(()=>{
        window.$v3.doNothing('will remove window resize')
        setTimeout(()=>{
            window.removeEventListener('resize',resizeFn);
            obj=null;
        })
        
    })
}
export const clickMixin=function(obj,fn){
    
    let clickFn=function(e){
        obj.value=fn(e);
    }
    window.addEventListener('click',clickFn);
    window.MFC.onBeforeUnmount(()=>{
        window.$v3.doNothing('will remove click')
        setTimeout(()=>{
            window.removeEventListener('click',clickFn);
        })
        
    })
}

export const renderV2ComponentMixin=function(){
    return {
        renderV2Component(v2Comps, div,props$={},rootTag="div",tplConfig={}) {
            window.$v3.doNothing('comps--',v2Comps);
            let options=v2Comps;
    
            options.mixins=[].concat(options.mixins||[]).concat({
            
               methods:{
                   
                    $t(){
                        window.$v3.doNothing('this in $t---:',this)
    
                    }
                },
                created(){
                    this.$router=window.$v3.$appInstance.$router;
                    this.$route=window.$v3.$appInstance.$route;
                }
            })
            
            let compInstance = new window.$v3.$Mfc2({
                template:`<${rootTag}>${tplConfig.before||''}
                <child v-bind="props$"></child>${tplConfig.after||''}</${rootTag}>`,
                data(){
                    return {
                        window$:window,
                        props$:props$
                    }
                },
                components:{
                    child:options
                }
            });
    
    
            compInstance.$mount(div);
            
          },
    }
}

// if(location.hostname=='127.0.0.1'){
//     Object.prototype.toRaw=function(){
//         return window.MFC.toRaw(this)
//     }
// }









/**
 * 以下方法以后可能用到：
 * 
 * 
 * window.__IMPORT = function (path) {

  if (!window.__IMPORT._IFRAME_INSTANCE_RESOLVER) {
    window.__IMPORT._IFRAME_INSTANCE_RESOLVER = {}
  }



  let prom = new Promise(async (resolve) => {
    window.__IMPORT._IFRAME_INSTANCE_RESOLVER[path] = resolve;
    resolve.IFRAME_INSTANCE = document.createElement("iframe")
    resolve.IFRAME_INSTANCE.srcdoc = `<script>
 ;(async function(){
  let result=await import("${path}");
  let parentWindow=window.parent;
  parentWindow.__IMPORT._IFRAME_INSTANCE_RESOLVER["${path}"](result)
  setTimeout(()=>{
    parentWindow.__IMPORT._IFRAME_INSTANCE_RESOLVER["${path}"].IFRAME_INSTANCE.parentNode.removeChild(
      parentWindow.__IMPORT._IFRAME_INSTANCE_RESOLVER["${path}"].IFRAME_INSTANCE)
      delete parentWindow.__IMPORT._IFRAME_INSTANCE_RESOLVER["${path}"]
      if(Object.keys(parentWindow.__IMPORT._IFRAME_INSTANCE_RESOLVER)){
        //delete parentWindow.__IMPORT._IFRAME_INSTANCE_RESOLVER
      }
  })

 })();
  // window.__IMPORT._IFRAME_IMPORT_RESULT=result;
  </script>`
    document.body.appendChild(resolve.IFRAME_INSTANCE);

  })

  return prom;


};
 * 
 */


/** 
 * copy webpack plugin demo:
 * const CopyWebpackPlugin = require("copy-webpack-plugin");
 * configureWebpack: config => {
        config.entry.app = ["babel-polyfill", "whatwg-fetch", "./src/main.js"];
        config.plugins.push(
            new ThemeColorReplacer({
                fileName: 'css/theme-colors-[contenthash:8].css',
                matchColors: getThemeColors(),
                injectCss: true,
                resolveCss
            })
        );
        config.plugins.push(
            new CopyWebpackPlugin([ // 复制插件 ， 只要时复制static文件夹中文件到config.dev.assetsSubDirectory文件夹下
                {
                  from: path.resolve(__dirname, './mfc_comps'),
                  to: './mfc_comps',
                  ignore: ['.*'] //忽视.*文件
                }
              ])
        )
    },
 */